import { EnumBusinessTripType, EnumKnowledgeExtendType, EnumMemberSalaryType, EnumOvertimeType, EnumScene, EnumWelfare, EnumWorkTime, type KnowledgeDetailEditRequest, type KnowledgeDetailResult } from "@/api/siderBar/model";
import { message } from "ant-design-vue";
import type { EntryType, EntryTypeOptionMultiple, EntryTypeWelfare } from "./types";

export function createEntry(basic: KnowledgeDetailResult, index: number): EntryType {
    const genericEntry = <EntryType>Object.assign({index}, basic)
    if (genericEntry.scene === EnumScene.工作时间) {
        genericEntry.ui = {
            restMode: EnumWorkTime.双休,
        }
        if (genericEntry.extend) {
            try {
                const patch = <NonNullable<typeof genericEntry.extendParams>>JSON.parse(genericEntry.extend)
                genericEntry.ui.restMode = patch.workTime
                genericEntry.ui.workStart = dateUtil(patch.startTime, "HH:mm")
                genericEntry.ui.workEnd = dateUtil(patch.endTime, "HH:mm")
            } catch (error) {
                console.log(error)
            }
        }
    } else if (genericEntry.scene === EnumScene.加班) {
        genericEntry.ui = {
            restMode: EnumOvertimeType.不接受加班
        }
        if (genericEntry.extend) {
            try {
                genericEntry.ui.restMode = <NonNullable<typeof genericEntry.extendParams>>JSON.parse(genericEntry.extend)
            } catch (error) {
                console.log(error)
            }
        }
    } else if (genericEntry.scene === EnumScene.工作地点) {
        genericEntry.ui = {}
        if (genericEntry.extend) {
            try {
                const patch = <NonNullable<typeof genericEntry.extendParams>>JSON.parse(genericEntry.extend)
                genericEntry.ui.place = patch.jobAddressId
            } catch (error) {
                console.log(error)
            }
        }
    } else if (genericEntry.scene === EnumScene.薪酬) {
        genericEntry.ui = {
            composition: EnumMemberSalaryType.固定薪资
        }
        if (genericEntry.extend) {
            try {
                const patch = <NonNullable<typeof genericEntry.extendParams>>JSON.parse(genericEntry.extend)
                genericEntry.ui.composition = patch.salaryType
                if (patch.salaryType === EnumMemberSalaryType.固定薪资) {
                    genericEntry.ui.salary = patch.salary
                } else {
                    genericEntry.ui.salary = patch.salary
                    genericEntry.ui.salaryMin = patch.salaryMin
                }
            } catch (error) {
                console.log(error)
            }
        }
    } else if (genericEntry.scene === EnumScene.试用期) {
        genericEntry.ui = {}
        if (genericEntry.extend) {
            try {
                const patch = <NonNullable<typeof genericEntry.extendParams>>JSON.parse(genericEntry.extend)
                genericEntry.ui.duration = patch.month
                genericEntry.ui.salaryMin = patch.salary
            } catch (error) {
                console.log(error)
            }
        }
    } else if (genericEntry.scene === EnumScene.福利) {
        genericEntry.extendOptionsNormalized = [
            { value: EnumWelfare.五险一金, label: "五险一金" },
            { value: EnumWelfare.交通补贴, label: "交通补贴" },
            { value: EnumWelfare.住房补贴, label: "住房补贴" },
            { value: EnumWelfare.包住, label: "包住" },
            { value: EnumWelfare.包吃, label: "包吃" },
            { value: EnumWelfare.带薪年假, label: "带薪年假" },
            { value: EnumWelfare.餐补, label: "餐补" }
        ]
        genericEntry.ui = {
            chooses: []
        }
        if (genericEntry.extend) {
            try {
                const patch = <NonNullable<typeof genericEntry.extendParams>>JSON.parse(genericEntry.extend)
                genericEntry.ui.chooses = patch
            } catch (error) {
                console.log(error)
            }
        }
    } else if (genericEntry.scene === EnumScene.出差) {
        genericEntry.extendOptionsNormalized = [
            { value: EnumBusinessTripType.不按受出差, label: "不按受出差" },
            { value: EnumBusinessTripType.偶尔出差, label: "偶尔出差" },
            { value: EnumBusinessTripType.经常出差, label: "经常出差" },
            { value: EnumBusinessTripType.频繁出差, label: "频繁出差" }
        ]
        genericEntry.ui = {}
        if (genericEntry.extend) {
            try {
                const patch = <NonNullable<typeof genericEntry.extendParams>>JSON.parse(genericEntry.extend)
                genericEntry.ui.choose = patch
            } catch (error) {
                console.log(error)
            }
        }
    } else if (genericEntry.scene === EnumScene.扩展 && genericEntry.extendType === EnumKnowledgeExtendType.单选) {
        if (genericEntry.extendOptions) {
            genericEntry.extendOptionsNormalized = (genericEntry.extendOptions as string[])
                .map(l => ({value: l, label: l}))
        }
        genericEntry.ui = {}
        if (genericEntry.extend) {
            try {
                const [patch] = <NonNullable<typeof genericEntry.extendParams>>JSON.parse(genericEntry.extend)
                genericEntry.ui.choose = patch
            } catch (error) {
                console.log(error)
            }
        }
    } else if (genericEntry.scene === EnumScene.扩展 && genericEntry.extendType === EnumKnowledgeExtendType.多选) {
        if (genericEntry.extendOptions) {
            genericEntry.extendOptionsNormalized = (genericEntry.extendOptions as string[])
                .map(l => ({value: l, label: l}))
        }
        genericEntry.ui = {
            chooses: []
        }
        if (genericEntry.extend) {
            try {
                const patch = <NonNullable<typeof genericEntry.extendParams>>JSON.parse(genericEntry.extend)
                genericEntry.ui.chooses = patch
            } catch (error) {
                console.log(error)
            }
        }
    } else {
        genericEntry.ui = {}
    }
    genericEntry.ui.reply = genericEntry.answer
    return genericEntry
}
export function validateEntry(genericEntry: EntryType): KnowledgeDetailEditRequest | false {
    if (genericEntry.scene === EnumScene.工作时间) {
        const { restMode, workStart, workEnd } = genericEntry.ui
        if (!isSetValue(restMode)) {
            message.error('请选择休息制度')
            return false
        }
        if (!isSetValue(workStart) || !isSetValue(workEnd)) {
            message.error('请选择工作时间')
            return false
        }
        genericEntry.extendParams = {
            workTime: restMode,
            startTime: workStart.format('HH:mm'),
            endTime: workStart.format('HH:mm'),
        }
    } else if (genericEntry.scene === EnumScene.加班) {
        const { restMode } = genericEntry.ui
        if (!isSetValue(restMode)) {
            message.error('请选择休息制度')
            return false
        }
        genericEntry.extendParams = restMode
    } else if (genericEntry.scene === EnumScene.工作地点) {
        const { place } = genericEntry.ui
        if (!isSetValue(place)) {
            message.error('请选择工作地址')
            return false
        }
        genericEntry.extendParams = {
            jobAddressId: place
        }
    } else if (genericEntry.scene === EnumScene.薪酬) {
        const { composition, salary, salaryMin } = genericEntry.ui
        if (!isSetValue(composition)) {
            message.error('请选择薪资结构')
            return false
        }
        genericEntry.extendParams = {
            salaryType: composition,
        }
        if (composition === EnumMemberSalaryType.固定薪资) {
            if (!isSetValue(salary)) {
                message.error('请输入固定薪资')
                return false
            }
            genericEntry.extendParams.salary = salary
        } else {
            if (!isSetValue(salaryMin)) {
                message.error('请输入最低底薪')
                return false
            }
            genericEntry.extendParams.salaryMin = salaryMin
            if (!isSetValue(salary)) {
                message.error('请输入一般收入')
                return false
            }
            genericEntry.extendParams.salary = salary
        }
    } else if (genericEntry.scene === EnumScene.试用期) {
        const { duration, salaryMin } = genericEntry.ui
        if (!isSetValue(duration)) {
            message.error('请输入试用期时长')
            return false
        }
        if (!isSetValue(salaryMin)) {
            message.error('请输入试用期底薪')
            return false
        }
        genericEntry.extendParams = {
            month: Number(duration),
            salary: Number(salaryMin)
        }
    } else if (genericEntry.scene === EnumScene.福利) {
        const { chooses } = genericEntry.ui
        if (!isSetValue(chooses) || chooses.length <= 0) {
            message.error('请选择')
            return false
        }
        genericEntry.extendParams = chooses
    } else if (genericEntry.scene === EnumScene.出差) {
        const { choose } = genericEntry.ui
        if (!isSetValue(choose)) {
            message.error('请选择')
            return false
        }
        genericEntry.extendParams = choose
    } else if (genericEntry.scene === EnumScene.扩展 && genericEntry.extendType === EnumKnowledgeExtendType.单选) {
        const { choose } = genericEntry.ui
        if (!isSetValue(choose)) {
            message.error('请选择')
            return false
        }
        genericEntry.extendParams = [choose]
    } else if (genericEntry.scene === EnumScene.扩展 && genericEntry.extendType === EnumKnowledgeExtendType.多选) {
        const { chooses } = genericEntry.ui
        if (!isSetValue(chooses) || chooses.length <= 0) {
            message.error('请选择')
            return false
        }
        genericEntry.extendParams = chooses
    }
    const { reply } = genericEntry.ui
    if (!isSetValue(reply)) {
        message.error('请填写回复内容')
        return false
    }
    return {
        answer: reply,
        extend: "extendParams" in genericEntry && isSetValue(genericEntry.extendParams) ? JSON.stringify(genericEntry.extendParams) : void 0,
        question: genericEntry.question
    }
}
export function handleSelectMultiple<T extends EntryTypeOptionMultiple | EntryTypeWelfare>(_entry: MaybeRef<T>, value: T['ui']['chooses'][number]) {
    const entry = toValue(_entry)
    if (!entry) return
    const index = entry.ui.chooses.indexOf(value)
    if (~index) {
        entry.ui.chooses.splice(index, 1)
    } else {
        entry.ui.chooses.push(value)
    }
}
export const workTimeOptions: IOption<EnumWorkTime>[] = [
    { value: EnumWorkTime.双休, label: '双休' },
    { value: EnumWorkTime.大小周, label: '大小周' },
    { value: EnumWorkTime.单休, label: '单休' },
    { value: EnumWorkTime.早晚轮班, label: '排班轮休' },
    { value: EnumWorkTime.夜班, label: '夜班' }
]
export const workContinueOptions: IOption<EnumOvertimeType>[] = [
    { value: EnumOvertimeType.不接受加班, label: '不加班' },
    { value: EnumOvertimeType.偶尔加班, label: '偶尔加班' },
    { value: EnumOvertimeType.适度加班, label: '适度加班' },
    { value: EnumOvertimeType.经常加班, label: '经常加班' }
]
export const salaryOptions: IOption<EnumMemberSalaryType>[] = [
    { value: EnumMemberSalaryType.固定薪资, label: '固定薪资' },
    { value: EnumMemberSalaryType.薪资_提成, label: '底薪+提成' },
    { value: EnumMemberSalaryType.底薪_绩效, label: '底薪+绩效' },
    { value: EnumMemberSalaryType.底薪_绩效_提成, label: '底薪+绩效+提成' }
]